package com.ruoyi.user.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * @author houyong
 * 日期工具类
 */
public class DateUtils {

    public static final String PATTERN_1 = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_2 = "yyyy-MM-dd";

    public static final String PATTERN_3 = "HH:mm";

    public static final String PATTERN_4 = "M月d日";

    public static final String PATTERN_5 = "MM/dd/yyyy";

    public static final String PATTERN_6 = "yyyy-MM-dd HH:mm";

    public static final String PATTERN_7 = "yyyyMMddHHmmss";

    public static long str2DateLong(String dateStr) {
        try {
            return callbackDateFormat(PATTERN_1).parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 10位字符串日期返回参数日期当天最小毫秒数
     *
     * @param dateStr "yyyy-MM-dd" 2004-12-21
     * @return 参数日期当天最小毫秒数
     */
    public static long str2DateL_new(String dateStr) {
        try {
            return callbackDateFormat(PATTERN_5).parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static long str2DateL(String dateStr) {
        try {
            return callbackDateFormat(PATTERN_1).parse(dateStr + " 00:00:00").getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 10位字符串日期返回参数日期当天最大时间毫秒数
     *
     * @param dateStr "yyyy-MM-dd" 2004-12-21
     * @return 参数日期当天最大时间毫秒数
     */
    public static long str2EDateL(String dateStr) {
        try {
            return callbackDateFormat(PATTERN_1).parse(dateStr + " 23:59:59").getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * long 数字时间转换为 yyyy-MM-dd 格式字符串
     *
     * @param milltime
     * @return
     */
    public static String long2Str(Long milltime, String pattern) {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        if (milltime == null) {
            return null;
        } else {
            cal.setTimeInMillis(milltime);
            return callbackDateFormat(pattern).format(cal.getTime());
        }
    }

    public static Long currTimeLong() {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        return cal.getTimeInMillis();
    }

    private static DateFormat callbackDateFormat(String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        //dateFormat.setLenient(false);
        return dateFormat;
    }

    public static Long str2Long(String dateStr, String pattern) {
        try {
            return callbackDateFormat(pattern).parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }


    public static String getCurrentDate() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        return f.format(date.getTime());
    }


    public static Long getTodayStartTimer() {
        return str2DateL(getCurrentDate());
    }
}
